package com.example.springboot.controller.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot.common.Result;
import com.example.springboot.controller.dto.ArticleDTO;
import com.example.springboot.controller.dto.FollowDTO;
import com.example.springboot.entity.Articles;
import com.example.springboot.entity.Follows;
import com.example.springboot.entity.Likes;
import com.example.springboot.entity.User;
import com.example.springboot.mapper.FollowsMapper;
import com.example.springboot.service.IFollowsService;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
/**
 * <p>
 * 关注表 API控制器 - 不需要token验证的API
 * </p>
 *
 * @author
 */
@RestController
@RequestMapping("api/follow")
public class FollowApi {

    @Resource
    private FollowsMapper followsMapper;
    @Resource
    private IFollowsService iFollowsService;

    //是否关注
    @PostMapping("/isFollow")
    public Result isLike(@RequestBody Follows follows) {
        // 参数校验
        if (follows.getFollowerId() == null || follows.getFollowingId() == null) {
            return Result.error("500","关注者ID和被关注者ID不能为空");
        }

        try {
            // 查询点赞记录
            Integer count = followsMapper.selectisFollow(follows.getFollowerId(), follows.getFollowingId());

            // 返回结果
            // 假设 selectisFollow 返回的是记录数(0表示未关注，1表示已关注)
            boolean isFollow = count != null && count > 0;

            return Result.success(isFollow);

        } catch (Exception e) {
            return Result.error("500","关注过了");
        }
    }


    //个人主页关注列表
    @GetMapping("/page")
    public Result personalPage(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam String followingId) {

        // 构造分页和排序条件
        Page<FollowDTO> pageInfo = new Page<>(page, pageSize);
        QueryWrapper<Follows> queryWrapper = new QueryWrapper<Follows>();
        if (!" ".equals(followingId)) {
            queryWrapper.eq("follower_id",followingId);
        }
        // 执行查询（自动关联用户表）
        Page<FollowDTO> result = iFollowsService.pageWithAuthor(pageInfo, queryWrapper);
        return Result.success(result);
    }


    // 关注
    @PostMapping("/follow")
    public Result save(@RequestBody Follows follows) {
        // 设置当前时间为点赞时间
        follows.setTime(LocalDateTime.now());  // 使用LocalDateTime记录精确时间

        try {
            iFollowsService.saveOrUpdate(follows);

            // 格式化时间用于返回给前端 (格式示例：12:30:45)
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
            String formattedTime = follows.getTime().format(formatter);

            return Result.success("关注成功，时间：" + formattedTime);
        } catch (Exception e) {
            return Result.error("500","点赞失败：");
        }
    }

    // 删除关注
    @PostMapping("/unFollow")
    public Result deleteLikes(@RequestBody Follows follows) {
        followsMapper.deleteisFollowById(follows.getFollowerId(), follows.getFollowingId());
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        followsMapper.deleteById(id);
        return Result.success();
    }
}
